//-lboost_filesystem-gcc-d
//-L/usr/local/lib
#include <fstream>
#include <boost/filesystem/path.hpp>
#include <iostream>
#include <string>
#include <algorithm>
#include <stdio.h>
#include <math.h>
#include <boost/filesystem/path.hpp>
#include "CmdLine/cmdargs.h"
#include "stlhelper.h"
#include "get_arg.h"
using std::string;
using std::ofstream;
using std::ifstream;
using std::cerr;
using std::cout;
using std::max;
using boost::filesystem::path;
#define DEFAULT_CODEML_PATH "/net/cpp-group/tools/paml/"




//_______________________________________________________________________________________

//		GetArguments

//				from command line
//				Created [9/13/2001]

//_______________________________________________________________________________________
bool GetArguments(int argc, char* argv[], t_program_args& args)
{
	// optional help directive
	CmdArgUsage opt_help('h', "help", 	"display this message", &cout);

	// required options
	const unsigned REQUIRED = CmdArg::isREQ | CmdArg::isVALREQ;
	CmdArgStr opt_job_category 	('j', "job_category", "<string>",
								"Postgres Batch table category for this job.",
								REQUIRED);
	CmdArgStr opt_output_table	('o', "output_table", "<string>",
								"Postgres table holding the results. See below.",
								REQUIRED);
	CmdArgStr opt_input_table 	('i', "input_table", "<string>",
								"Postgres table holding alignment data for gene pairs.",
								REQUIRED);
	
	// database parameters
	CmdArgStr opt_dbname		('d', "dbname", "<string>",
								"PostgreSQL database name.",
								REQUIRED);
	CmdArgStr opt_host			('h', "host", "<string>",
								"PostgreSQL database host computer address.");
	CmdArgStr opt_user			('u', "user", "<string>",
								"PostgreSQL database user name. Defaults to 'postgres'.");
	CmdArgStr opt_password		('p', "password", "<string>",
								"PostgreSQL database password.");
	CmdArgStr opt_login			('l', "login", "<string>",
								"Other PostgreSQL login parameters in the form of "
								"keyword = value. E.g \"port=1234\".");
	
//YN00	CmdArgStr opt_paml			('p', "paml_path", "<string>",
//YN00								"path to yn00. "
//YN00								"Defaults to " DEFAULT_CODEML_PATH "yn00.");
//YN00	CmdArgStr opt_tempdir		('t', "temp_directory", "<string>",
//YN00								"temp directory where paml does its work.");
	CmdArgStr opt_err_log		('e', "err_log", "<string>",
								"file for logging bl2seq failures "
								"(defaults to STDERR).");
	CmdArgSet opt_verbose		('v', "verbose",
								"issue lots more messages.");

	string exe_file_name =  boost::filesystem::path(*argv).leaf();
	


		// construct command line
	CmdLine  cmdline(exe_file_name.c_str(),
							&opt_job_category,
							&opt_output_table,
							&opt_input_table,
							&opt_dbname,
							&opt_host,
							&opt_user,
							&opt_password,
							&opt_login,
//YN00							&opt_paml,
//YN00							&opt_tempdir,
							&opt_err_log,
							&opt_help,
							&opt_verbose,
							0);

	// parse command line
	CmdArgvIter  argv_iter(--argc, ++argv);
	cmdline.description( "This program calculates GC content for specified gene pairs."
							"The following tables are required:\n\n"
							"TABLE: batch_jobs:\n"
							"(batch integer, checked_in boolean, checked_out boolean)\n\n"
							"TABLE: Input_table:\n"
							"(gene_id1, gene_id2, pi1, pi2, acc_code1, acc_code2, "
							"aln_gaps1, aln_gaps2, aln_begin, aln_end).\n\n"
							"TABLE Results_table:\n"
							"(gene_id1, gene_id2, pi1, pi2, acc_code1 TEXT, "
							"acc_code2 TEXT, gc_1, gc_2, gc3_1, gc3_2, error_str TEXT)\n\n"
							"If the log file name "
//YN00							"or the temp_directory "
							"contains the term %PID%, "
							"this will be replaced by the unique postgreSQL server process ID\n");
	if (cmdline.parse(argv_iter))
		return false;

	args.error_log_name= opt_err_log.str();
	
	
//YN00	args.paml		= opt_paml;
//YN00	if (! args.paml.length())
//YN00		args.paml= DEFAULT_CODEML_PATH "yn00";
//YN00	
//YN00	args.tempdir	= opt_tempdir;
	
	// make sure the output file is valid
	args.job_category		= opt_job_category.str();
	args.output_table		= opt_output_table.str();
	args.input_table		= opt_input_table.str();

	// define verbose first before any error messages generated
	args.verbose	= opt_verbose;


	// initialize postgresql login
	args.db_login = "dbname=" +opt_dbname.str();
	
	string user		= opt_user;
	if (opt_user.str().length())
		args.db_login += " user=" + opt_user.str();
	else
		args.db_login += " user=postgres";
	
	if (opt_host.str().length())
		args.db_login += " host=" + opt_host.str();
	
	if (opt_password.str().length())
		args.db_login += " password=" + opt_password.str();

	if (opt_login.str().length())
		args.db_login += ' ' + opt_login.str();
	
	return true;
}


bool t_program_args::open_log_files(const std::string& PID)
{
	str_search_and_replace(error_log_name, "%PID%", PID);
	
	// make sure the error log file is valid
	
	log_to_file		= error_log_name.length();
	if (log_to_file)
	{
		f_err_log.open(error_log_name.c_str(), std::ios_base::out | std::ios_base::ate);
		if (!f_err_log.good())
		{
			cerr << "ERROR!! Could not open the error log file (" << error_log_name << ").\n";
			return(false);
		}
	}
	return true;
}

